#include "StdAfx.h"

#include "Approach.h"
#include "EphemWrapper.h"

CApproach::CApproach(eBodies bodyNum, double dTime, const SBody &body, double dXdiff[3], CAsteroidOrbitParam::Ptr pOrbitParam)
: m_bodyNum(bodyNum)
, m_dTime(dTime)
, m_body(body)
, m_pAsteroidOrbitParam(pOrbitParam)
{
	memcpy(m_dXdiff, dXdiff, sizeof(m_dXdiff));

	m_dDist = 0.0;
	for(int i = 0; i < sizeof(m_body.x)/sizeof(m_body.x[0]); i++)
		m_dDist += m_dXdiff[i] * m_dXdiff[i];
	
	m_dDist = sqrt(m_dDist);
	//printf("!!!Found at %s, dist %lf km\n", CEphemWrapper::StrDateFromJD(dTime).c_str(), CEphemWrapper::AUtoKM(Dist()));
}

CApproach::~CApproach(void)
{
}

double CApproach::Dist() const
{
	return m_dDist;
}

bool CApproach::CompareApproachesByTime(Ptr a, Ptr b)
{
	return a->m_dTime < b->m_dTime;
}

bool CApproach::CompareApproachesByTimeParam(Ptr a, Ptr b)
{
	if( a->m_dTime == b->m_dTime)
		return a->m_pAsteroidOrbitParam->GetVariousParam() < b->m_pAsteroidOrbitParam->GetVariousParam();
	else
		return a->m_dTime < b->m_dTime;
}

void PrintDate(double dDate);

void CApproach::Print() const
{
	printf(" approach with: %s\n", CEphemWrapper::GetBodyName(m_bodyNum));
	PrintDate(m_dTime);
	printf(" distance=%10.7lf ae.  %15.2lf km.\n", m_dDist, CEphemWrapper::AUtoKM(m_dDist));
	printf(" position: x=%14.2lf km.  y=%14.2lf km.  z=%14.2lf km.\n\n", 
		CEphemWrapper::AUtoKM(m_dXdiff[0]),
		CEphemWrapper::AUtoKM(m_dXdiff[1]),
		CEphemWrapper::AUtoKM(m_dXdiff[2]));
}

